package org.baicaix.demo.data.bind {
	import org.baicaix.data.bind.BindableObject;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * 使用方法：
		1.首先创建一个BindableObject
		2.调取它的bind方法，绑定到要更新的对象，比如：bindExpObj.bind(main.txt1,"text");
		3.在需要的时候，对数据源更新，比如：bindExpObj.property = main.stringSRC.text;
		4.解除绑定，使用unlock方法，参数与bind相同，比如：bindExpObj.unlock(main.txt1,"text");
	 * @author baicaix
	 */
	[SWF(width=400, height=400, frameRate=25,backgroundColor=0xFFFFFF)]
	public class AS3BindTest extends Sprite {

		private var main : MainContainer = new MainContainer();
		private var bindExpObj : BindableObject = new BindableObject("");
		private var cb : NeoComboBox = new NeoComboBox();
		
		public function AS3BindTest() {

			super();
			cb.selected = true;
			cb.y = 10;
			cb.x = 320;
			cb.addEventListener(MouseEvent.CLICK, unlockBind);
			main.stringSRC.addEventListener(Event.CHANGE, dataChange);
			bindExpObj.bind(main.txt1, "text");
			bindExpObj.bind(main.txt2, "text");
			bindExpObj.bind(main.txt3, "text");
			bindExpObj.bind(main.mc.txt, "text");
			addChild(main);
			main.addChild(cb);
		}

		private function dataChange(...args) : void {
			bindExpObj.property = main.stringSRC.text;
		}

		private function unlockBind(...args) : void {
			if(cb.selected == true) {
				bindExpObj.unlock(main.txt1, "text");
				bindExpObj.unlock(main.txt2, "text");
				bindExpObj.unlock(main.txt3, "text");
				bindExpObj.unlock(main.mc.txt, "text");
				cb.selected = false;
				cb.tip.visible = false;
			} else {
				bindExpObj.bind(main.txt1, "text");
				bindExpObj.bind(main.txt2, "text");
				bindExpObj.bind(main.txt3, "text");
				bindExpObj.bind(main.mc.txt, "text");
				cb.selected = true;
				cb.tip.visible = true;
			}
		}
	}
}
